package com.loie.xcx;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.config.TaxCloudConfig;
import com.loie.xcx.mapper.FkblMapper;
import com.loie.xcx.mapper.SqlMapper;
import com.loie.xcx.model.bankdto.Shbank_OrdrRgst;
import com.loie.xcx.util.HttpUtil;
import com.loie.xcx.util.RequestUtil;
import com.loie.xcx.util.SecureUtil;
import com.loie.xcx.web.DataCenterApplication;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = DataCenterApplication.class)
@Slf4j

@MapperScan("com.loie.xcx.mapper")

public
class json_test {
    @Autowired
    TaxCloudConfig taxCloudConfig;
    @Autowired
    RequestUtil requestUtil;

    @Autowired
    SqlMapper sqlMapper;

    @Autowired
    FkblMapper fkblMapper;
    @Test
    public void test_getztlist(){
        List<Shbank_OrdrRgst> shbankOrdrRgstList=fkblMapper.getshbank_ddzt("");
        System.out.println(shbankOrdrRgstList);
    }

    @Test
    public void test_queryStdDuePayResult() throws  Exception{
        String url = "http://127.0.0.1:8090/QianchengAPI/SHAPI/SendToSpdb/queryStdDuePayResult";
        JSONObject jsonObject = new JSONObject();
        String  sourceAssetNo="WEC-20240814173215950";
        jsonObject.put("sourceAssetNo",sourceAssetNo);
        //jsonObject.put("corpserialnos","AU062");
        HashMap<String, String> header = new HashMap<>();

        header.put("msg", jsonObject.toJSONString());
        String response = HttpUtil.httpAjaxPost(url, header, false);
        System.out.println(response);
    }

    @Test
    public void test_handle() throws IOException {
      //  String url = "http://127.0.0.1:8090/QianchengAPI/SHAPI/handle";
        String url = "http://122.225.247.198:8099/QianchengAPI/SHAPI/handle";
        String path="/Apidoc/WEC-20240806112647387.json";
        Resource resource = new ClassPathResource(path);

        byte[] bytes = Files.readAllBytes(Paths.get(resource.getURI()));
        String ret=new String(bytes, StandardCharsets.UTF_8);
        System.out.println(ret);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        String response = com.loie.xcx.util.HttpUtil.httpPostJson(url, headers, ret, false);
        System.out.println(response);


    }


    @Test
    public void test_attachment() throws IOException {
        String url = "http://127.0.0.1:9200/xcxAPI/purchaseorders/getAttachmentList";
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("recordId", "A5F15F47-9B31-4D1F-B7FB-7754C974FCC8");
        jsonObject.put("module", "采购合同");
        String response = HttpUtil.httpPostJson(url, headers, jsonObject.toJSONString(), false);

// 处理响应
        if (response != null) {
            // 成功
            System.out.println(response);
            JSONObject jsonresult = JSONObject.parseObject(response);
            JSONArray array = JSONArray.parseArray(jsonresult.getJSONObject("data").getString("attachmentList"));
            System.out.println("array" + array);
            System.out.println(array.size());
            for (int i = 0; i <= array.size() - 1; i++) {

                JSONObject json_attachment = array.getJSONObject(i);
                //System.out.println("子项"+json_attachment);
                String base64str = json_attachment.getString("base64");
                String name = json_attachment.getString("name");
                System.out.println(name);

                if (!Strings.isEmpty(name) && !Strings.isEmpty(base64str)) {
                    byte[] decodedBytes = Base64.getDecoder().decode(base64str);
                    FileOutputStream fos = new FileOutputStream("D:\\" + name);
                    fos.write(decodedBytes);
                    fos.close();
                }

            }


        } else {
            // 请求失败
            System.out.println("Request failed");
        }

    }

    @Test
    public void login_test() {
        String url = "http://127.0.0.1:9200/xcxAPI/sys/login";

// 构建请求头
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Xcx-Timestamp", String.valueOf(System.currentTimeMillis()));
        headers.put("Xcx-Nonce", "xcxbj202310010001");
        headers.put("Xcx-AppKey", "1223334444");
        headers.put("Content-Type", "application/json");
        System.out.println(headers);
        String appSecret = "AAB2CDC7-6E8B-C012-62AC-A8E7D0C8F501";
        String jsonBody = "{\"openId\":\"123456789\"}";
        String xcxAppKey = headers.get("Xcx-AppKey");
        String xcxTimestamp = headers.get("Xcx-Timestamp");
        String xcxNonce = headers.get("Xcx-Nonce");
        //String xcxSignature = request.getHeader("Xcx-Signature");

        StringBuilder str = new StringBuilder();
        str.append(xcxTimestamp).append(xcxNonce)
                .append(xcxAppKey).append(appSecret).append(jsonBody);
        String md5 = SecureUtil.MD5Encode(str.toString());
        System.out.println(str);
        headers.put("Xcx-Signature", md5);

// 构建请求体


// 发送POST JSON请求
        String response = HttpUtil.httpPostJson(url, headers, jsonBody, false);

// 处理响应
        if (response != null) {
            // 成功
            System.out.println(response);
        } else {
            // 请求失败
            System.out.println("Request failed");
        }

    }

    @Test
    public void test_getFlowApprove() throws Exception {

//        String url = "http://122.227.233.90:9300/xcxAPI/workflow/getFlowCompletedList";

        String url = "http://127.0.0.1:9300/xcxAPI/workflow/flowApprove";

        //{"recordId":"E7865D0A-0E6D-4E9E-9EA4-02471F8A6C31","modulename":"采购合同","subject":"采购合同:23JP0340","isgree":true,"openId":"jz","content":"******注意及时交货，价格是否已达到最优"}
        //url = "http://127.0.0.1:9300/xcxAPI/workflow/getFlowTaskList";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("openId", "jz");
        jsonObject.put("recordId", "6130406D-8B2D-4A64-BD00-2435E306E93D");
        jsonObject.put("modulename", "公司客户");
        jsonObject.put("subject", "Gerencia NonFood Regional Cencosud<业务管理部/业务管理部>");
        jsonObject.put("isgree", true);

        jsonObject.put("content", "OK");

        //jsonObject.put("InstanceID","D67CA464-8B6B-46C7-A9C5-A0BD26B91829");
        System.out.println(taxCloudConfig.getAppSecret());
        String response = requestUtil.getRequestData(url, "POST", jsonObject.toJSONString());
        if (response != null) {

            //System.out.println(response);
            JSONObject jsonresult = JSONObject.parseObject(response);
            String data = SecureUtil.decryptAES(jsonresult.getString("data"), taxCloudConfig.getAppSecret());

            System.out.println(data);
        }

    }

    @Test
    public void test_getItemInfoByItemNoandPermissions() throws Exception {

//        String url = "http://122.227.233.90:9300/xcxAPI/workflow/getFlowCompletedList";

       // String url = "http://127.0.0.1:9300/xcxAPI/order/getItemByPermissionslist";
        String url = "http://127.0.0.1:9300/xcxAPI/order/getItemInfoByItemNoandPermissions";


        //{"recordId":"E7865D0A-0E6D-4E9E-9EA4-02471F8A6C31","modulename":"采购合同","subject":"采购合同:23JP0340","isgree":true,"openId":"jz","content":"******注意及时交货，价格是否已达到最优"}
        //url = "http://127.0.0.1:9300/xcxAPI/workflow/getFlowTaskList";
        //String url = "http://127.0.0.1:9300/xcxAPI/order/getItemInfoByItemNo";
        //String url = "http://127.0.0.1:9300/xcxAPI/purchaseorders/getCorporateCustomersdetail";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("openId", "wy");
        jsonObject.put("itemNo", "HLMAX1");
//        jsonObject.put("module", "公司客户");
//        jsonObject.put("recordId", "C33DF8DF-A6C5-421F-86AB-AB0AA550A402");
//        jsonObject.put("pageIndex", -1);
        //jsonObject.put("pageSize", 30);



        //jsonObject.put("InstanceID","D67CA464-8B6B-46C7-A9C5-A0BD26B91829");
        System.out.println(taxCloudConfig.getAppSecret());
        String response = requestUtil.getRequestData(url, "POST", jsonObject.toJSONString());
        if (response != null) {

            //System.out.println(response);
            JSONObject jsonresult = JSONObject.parseObject(response);
            String data = SecureUtil.decryptAES(jsonresult.getString("data"), taxCloudConfig.getAppSecret());

            System.out.println(data);
        }

    }

    @Test
    public void tes_username(){
        Map<String, Object> params = new HashMap<>();
       String recordid="C33DF8DF-A6C5-421F-86AB-AB0AA550A402";

        params.clear();
        params.put("sql", "select dbo.getusername(userid) username from Corporate_Customers where recordid=#{recordid}");
        params.put("recordid", recordid);

        String username = sqlMapper.dic_type(params);
        System.out.println(username);
    }


}
